﻿using System.Web.Mvc;
using FShare.ViewModels;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using FShare.DataModel;
using System;

namespace FShare.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        /// <summary>
        /// Shows a list of uploaded files of the current user.
        /// User must be logged in to access this method.
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult MyFiles()
        {
            var model = new List<UploadedFileViewModel>();
            var user = Membership.GetUser(true);
            using (var context = new FShareDataModelContainer())
            {
                var files = context.FShareFiles
                    .Where(f => f.Uploader.UserName == user.UserName && !f.IsDeleted);
                foreach (var file in files)
                {
                    model.Add(new UploadedFileViewModel
                    {
                        DownloadSecret = file.DownloadSecret,
                        DeleteSecret = file.DeleteSecret,
                        FileName = file.Name,
                        Description = file.Description,
                        NumberOfDownloads = file.NumberOfDownloads,
                        UploadedTime = file.DateUploaded
                    });
                }
            }
            return View(model);
        }
    }
}
